


import 'package:bike_ride/api/common_repository.dart';
import 'package:bike_ride/common/mvvm/base_view_model.dart';
import 'package:bike_ride/common/util/account_manager.dart';
import 'package:bike_ride/common/widget/notifier_widget.dart';
import 'package:bike_ride/model/protocol/model/contract_m.dart';
import 'package:bike_ride/model/protocol/view/protocol_v.dart';
import 'package:bike_ride/model/question/model/question_m.dart';
import 'package:bike_ride/generated/l10n.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

class ProtocolViewModel extends PageViewModel<ProtocolViewState> {
  CancelToken? cancelToken;
  ValueNotifier<List<QuestionM>> helpList = ValueNotifier<List<QuestionM>>(List.empty());
  List<ContractM> _contracts = List.empty();

  /// 协议列表
  List<ContractM> get contracts => [
    ContractM(
      name: S.current.agreeProtocolB,
      signed: 1,
      linkUrl: AccountManager.privacyProtocolUrl,
    ),
    ContractM(
      name: S.current.agreeProtocolA,
      signed: 1,
      linkUrl: AccountManager.useProtocolUrl,
    ),
    ..._contracts
  ];

  @override
  onCreate() {
    cancelToken = CancelToken();
    pageDataModel = PageDataModel();
    getContracts();
  }

  @override
  onDispose() {
    if (!(cancelToken?.isCancelled ?? true)) {
      cancelToken?.cancel();
    }
    super.onDispose();
  }


  Future<bool> signContracts(num id) async {
    return CommonRepository.signContractsIdl(contractIds: [id]);
  }

  Future getContracts() async {
    final result = await CommonRepository.getContractsIdl();
    _contracts = result;
    state.notifyChange();
  }

  /// 请求数据
  @override
  Future<PageViewModel?> requestData({Map<String, dynamic>? params}) async {
    return null;

  }
}